﻿using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;

namespace BearUtil
{
    /// <summary>
    /// 故事对话需要的一些设置参数
    /// </summary>
    [Serializable]
    public class DialogProperty
    {
        /// <summary>
        /// 触发事件类型
        /// </summary>
        [HideInInspector, Obsolete("旧版本数据")]
        public StoryTriggerEvent trigger_event;
        
        /// <summary>
        /// 触发参数
        /// </summary>
        [HideInInspector, Obsolete("旧版本数据")]
        public string trigger_param;
        
        [LabelText("触发事件")]
        public List<StoryTriggerEvent> triggerEvents;
        [LabelText("触发参数")]
        public List<int> triggerParams;
        
        
        /// <summary>
        /// 触发事件参数
        /// </summary>
        [LabelText("地图id")]
        public int map_id;
        
        /// <summary>
        /// 相机位置
        /// </summary>
        [LabelText("相机位置")]
        public Vector2 camera_pos;
        
        /// <summary>
        /// 主角位置
        /// </summary>
        [LabelText("主角位置")]
        public Vector2 pc_pos;

        /// <summary>
        /// 主角朝向
        /// </summary>
        [LabelText("主角朝向")]
        public ObjectDirection pc_direction = ObjectDirection.Down;

        /// <summary>
        /// 对话梗概
        /// </summary>
        [LabelText("对话梗概")]
        public string dialogue_gist;

        /// <summary>
        /// 备注
        /// </summary>
        [LabelText("备注")]
        public string remark;
        
        /// <summary>
        /// excel 相关表格
        /// </summary>
        [LabelText("相关文本表格"), OnValueChanged("analysisData")]
        public ExcelData LineData;

        private List<string> lineKeys;
        
        // 文本 key 值
        private List<string> Lines
        {
            get
            {
                if (lineKeys == null)
                    lineKeys = new List<string>();
                
                return lineKeys;
            }
        }

        public List<string> GetLines()
        {
            if (Lines != null && Lines.Count <= 0)
                analysisData();
            
            return Lines;
        }

        /// <summary>
        /// 分析数据
        /// </summary>
        private void analysisData()
        {
            if (LineData == null)
                return;
            
            LineData.LoadTransformData();

            if (LineData.Datas == null || LineData.col <= 0 || LineData.row <= 0)
                return;
            
            Lines.Clear();
            for (int i = 3; i < LineData.row; i++)
            {
                Lines.Add(LineData.Datas[2, i]);
            }
        }
        
        /// <summary>
        /// 获取文本内容
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetLineContent(string key)
        {
            int type = (int)StoriesSetting.Instance.property.language;
            LineData.LoadTransformData();
            
            for (int y = 0; y < LineData.row; y++)
            {
                if (LineData.Datas[2, y] == key)
                {
                    return LineData.Datas[type, y];
                }
            }

            Debug.LogWarning(LineData.sheetName + " 未找到对应文本");
            return "";
        }
    }
}
